﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pixeldyne.Core.Extensions
{
	public static class Dictionary
	{
		public static void ChangeKey<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey oldKey, TKey newKey)
		{
			dict.Add(newKey, dict[oldKey]);
			dict.Remove(oldKey);
		}

		public static bool ContainsValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TValue item)
		{
			return dictionary.Values.Contains(item);
		}

		public static bool ContainsAny<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IEnumerable<TKey> keys)
		{
			return keys.Any(dictionary.ContainsKey);
		}

		public static bool ContainsAll<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IEnumerable<TKey> keys)
		{
			return keys.All(dictionary.ContainsKey);
		}

		public static string ItemsToString(this IEnumerable<string> list, string separator = null)
		{
			if (separator == null)
				separator = Environment.NewLine;

			var sb = new StringBuilder();

			foreach (var item in list)
			{
				sb.Append(item);
				sb.Append(separator);
			}

			return sb.ToString(0, sb.Length - separator.Length);
		}
	}
}
